// This is autogenerated file. Do not edit!

package models.autogenerated;

class LoginkeyManager
{
	var db : orm.Db;
	var orm : models.Orm;
	public var query(get, never) : orm.SqlQuery<models.Loginkey>;

	function get_query() : orm.SqlQuery<models.Loginkey>
	{
		return new orm.SqlQuery<models.Loginkey>("loginkey", db, this);
	}

	public function new(db:orm.Db, orm:models.Orm) : Void
	{
		this.db = db;
		this.orm = orm;
	}

	function newModelFromParams(key:String, user_id:String) : models.Loginkey
	{
		var _obj = new models.Loginkey(db, orm);
		_obj.key = key;
		_obj.user_id = user_id;
		return _obj;
	}

	function newModelFromRow(d:Dynamic) : models.Loginkey
	{
		var _obj = new models.Loginkey(db, orm);
		_obj.key = Reflect.field(d, 'key');
		_obj.user_id = Reflect.field(d, 'user_id');
		return _obj;
	}

	public function where(field:String, op:String, value:Dynamic) : orm.SqlQuery<models.Loginkey>
	{
		return query.where(field, op, value);
	}

	public function get(key:String) : models.Loginkey
	{
		return getBySqlOne('SELECT * FROM `loginkey` WHERE `key` = ' + db.quote(key));
	}

	public function create(key:String, user_id:String) : models.Loginkey
	{
		db.query('INSERT INTO `loginkey`(`key`, `user_id`) VALUES (' + db.quote(key) + ', ' + db.quote(user_id) + ')');
		return newModelFromParams(key, user_id);
	}

	public function createNamed(data:{ key:String, user_id:String }) : models.Loginkey
	{
		db.query('INSERT INTO `loginkey`(`key`, `user_id`) VALUES (' + db.quote(data.key) + ', ' + db.quote(data.user_id) + ')');
		return newModelFromParams(data.key, data.user_id);
	}

	public function createOptional(data:{ key:String, ?user_id:String }) : models.Loginkey
	{
		createOptionalNoReturn(data);
		return get(data.key);
	}

	public function createOptionalNoReturn(data:{ key:String, ?user_id:String }) : Void
	{
		var fields = [];
		var values = [];
		fields.push('`key`'); values.push(db.quote(data.key));
		if (Reflect.hasField(data, 'user_id')) { fields.push('`user_id`'); values.push(db.quote(data.user_id)); }
		db.query('INSERT INTO `loginkey`(' + fields.join(", ") + ') VALUES (' + values.join(", ") + ')');
	}

	public function delete(key:String) : Void
	{
		db.query('DELETE FROM `loginkey` WHERE `key` = ' + db.quote(key) + ' LIMIT 1');
	}

	public function getAll(_order:String=null) : Array<models.Loginkey>
	{
		return getBySqlMany('SELECT * FROM `loginkey`' + (_order != null ? ' ORDER BY ' + _order : ''));
	}

	public function getBySqlOne(sql:String) : models.Loginkey
	{
		var rows = db.query(sql + ' LIMIT 1');
		if (rows.length == 0) return null;
		return newModelFromRow(rows.next());
	}

	public function getBySqlMany(sql:String) : Array<models.Loginkey>
	{
		var rows = db.query(sql);
		var list : Array<models.Loginkey> = [];
		for (row in rows)
		{
			list.push(newModelFromRow(row));
		}
		return list;
	}
}